﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Creation;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Structure;

namespace YArchitech.Revit
{
	public class ConnectHelper
	{
		public static int CheckPositionBetweenElement(Connector connBase, Connector conn)
		{
			XYZ xyz = new XYZ(0.0, 0.0, connBase.Origin.Z);
			XYZ xyz2 = (new XYZ(0.0, 0.0, conn.Origin.Z) - xyz).Normalize();
			int result;
			if (connBase.CoordinateSystem.BasisZ.Normalize().IsAlmostEqualTo(xyz2))
			{
				result = 0;
			}
			else if (connBase.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, 1.0)))
			{
				result = 1;
			}
			else if (connBase.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0)))
			{
				result = 2;
			}
			else
			{
				result = 3;
			}
			return result;
		}

		public static XYZ GetConnPointOnDuct(Duct duct, XYZ pickPoint, PlanarFace connFace, int connType, ref XYZ faceVector)
		{
			XYZ xyz = null;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			double num = 0.0001;
			if (connType != 11)
			{
				if (connType == 22 || connType == 33)
				{
					Curve locationCurve = hypiping.GetLocationCurve();
					xyz = new YJKLine(locationCurve.GetEndPoint(0), locationCurve.GetEndPoint(1)).GetProject(pickPoint);
					faceVector = connFace.FaceNormal();
				}
			}
			else
			{
				xyz = connFace.Origin;
				XYZ xyz2 = hypiping.StartConnector.Origin;
				XYZ xyz3 = hypiping.EndConnector.Origin;
				xyz2 = new XYZ(xyz2.X, xyz2.Y, xyz.Z);
				xyz3 = new XYZ(xyz3.X, xyz3.Y, xyz.Z);
				if (xyz.IsAlmostEqualTo(xyz2, num))
				{
					faceVector = hypiping.StartConnector.Connector.CoordinateSystem.BasisZ;
				}
				else
				{
					faceVector = hypiping.EndConnector.Connector.CoordinateSystem.BasisZ;
				}
			}
			return xyz;
		}

		public static XYZ CalLayPoint(XYZ basePoint, XYZ vector, double distance)
		{
			distance = UnitConvert.CovertToAPI(distance, (Autodesk.Revit.DB.DisplayUnitType)2);
			return basePoint.Add(distance * vector);
		}

		public static TextNote CreatTextNote(Autodesk.Revit.DB.Document doc, string textNoteStr, XYZ layPoint)
		{
			TextNoteType textNodeType = ConnectHelper.GetTextNodeType(doc);
			return UnifiedModified.HYCreatTextNote(doc, textNoteStr, layPoint, textNodeType);
		}

		public static void GetTagTextWidthAndHeight(string tagText, Autodesk.Revit.DB.Document doc, ref double width, ref double height)
		{
			SubTransaction subTransaction = new SubTransaction(doc);
			try
			{
				subTransaction.Start();
				XYZ startPoint = new XYZ(0.0, 0.0, 0.0);
				TextNoteType textNodeType = ConnectHelper.GetTextNodeType(doc);
				TextNote textNote = UnifiedModified.HYCreatTextNote(doc, tagText, startPoint, textNodeType);
				doc.Regenerate();
				double num = (double)doc.ActiveView.Scale * 304.8;
				width = UnifiedModified.GetTextNoteWidth(doc, textNote) * 304.8;
				height = textNote.Height * num;
				subTransaction.RollBack();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				if (subTransaction != null && subTransaction.GetStatus() == 1)
				{
					subTransaction.RollBack();
				}
			}
		}

		private static TextNoteType GetTextNodeType(Autodesk.Revit.DB.Document doc)
		{
			ICollection<Element> collection = new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).ToElements();
			foreach (Element element in collection)
			{
				if (element.Name.Equals("HY-仿宋-3.0-0.7") || element.Name.Equals("HY-仿宋-3-0.7"))
				{
					return element as TextNoteType;
				}
			}
			string text = "HY-仿宋-3.0-0.7";
			TextNoteType textNoteType = (collection.First<Element>() as TextNoteType).Duplicate(text) as TextNoteType;
			textNoteType.get_Parameter(BuiltInParameter.TEXT_FONT).Set("仿宋");
			textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).Set(0.00984251968503937);
			textNoteType.get_Parameter(BuiltInParameter.LINE_COLOR).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_STYLE_BOLD).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_STYLE_ITALIC).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_STYLE_UNDERLINE).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).Set(0.7);
			textNoteType.get_Parameter(BuiltInParameter.LEADER_OFFSET_SHEET).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_BACKGROUND).Set(1);
			return textNoteType;
		}

		public static bool CheckOffSet(IList<XYZ> xyzs, double offSet, double angle)
		{
			bool result = true;
			if (xyzs == null || xyzs.Count < 2)
			{
				return false;
			}
			double num = Math.Tan(angle);
			if (num < 0.0001)
			{
				num = 0.0;
			}
			double num2 = 0.0;
			if (0.0 != num)
			{
				num2 = Math.Abs(offSet / num);
			}
			if (Math.Abs(num2) < 0.0001)
			{
				num2 = 0.0;
			}
			if (num2 * 2.0 >= xyzs[0].DistanceTo(xyzs[1]))
			{
				result = false;
			}
			return result;
		}

		public ElementSet LayFamilyInstance(List<FamilyInstanceCreationData> dataList, Autodesk.Revit.DB.Document doc)
		{
			return UnifiedModified.HYNewFamilyInstances(doc, dataList);
		}

		public ElementSet LayFamilyInstance(List<XYZ> xyzList, FamilySymbol familySymbol, StructuralType structuralType, Autodesk.Revit.DB.Document doc)
		{
			new List<FamilyInstanceCreationData>();
			ElementSet elementSet = new ElementSet();
			foreach (XYZ xyz in xyzList)
			{
				if (!familySymbol.IsActive)
				{
					familySymbol.Activate();
				}
				FamilyInstance familyInstance = doc.Create.NewFamilyInstance(xyz, familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
				if (familyInstance != null)
				{
					elementSet.Insert(familyInstance);
				}
			}
			if (elementSet != null || elementSet.Size > 0)
			{
				return elementSet;
			}
			return null;
		}

		public ElementSet LayFamilyInstance(List<XYZ> xyzList, FamilySymbol familySymbol, Level level, StructuralType structuralType, Autodesk.Revit.DB.Document doc)
		{
			List<FamilyInstanceCreationData> list = new List<FamilyInstanceCreationData>();
			foreach (XYZ xyz in xyzList)
			{
				if (xyz != null)
				{
					FamilyInstanceCreationData familyInstanceCreationData = new FamilyInstanceCreationData(xyz, familySymbol, level, structuralType);
					if (familyInstanceCreationData != null)
					{
						list.Add(familyInstanceCreationData);
					}
				}
			}
			if (list.Count > 0)
			{
				return this.LayFamilyInstance(list, doc);
			}
			return null;
		}

		public ElementSet LayFamilyInstance(Dictionary<XYZ, FamilySymbol> dic, Level level, StructuralType structuralType, Autodesk.Revit.DB.Document doc)
		{
			List<FamilyInstanceCreationData> list = new List<FamilyInstanceCreationData>();
			foreach (KeyValuePair<XYZ, FamilySymbol> keyValuePair in dic)
			{
				FamilyInstanceCreationData familyInstanceCreationData = new FamilyInstanceCreationData(keyValuePair.Key, keyValuePair.Value, level, structuralType);
				if (familyInstanceCreationData != null)
				{
					list.Add(familyInstanceCreationData);
				}
			}
			if (list.Count > 0)
			{
				return this.LayFamilyInstance(list, doc);
			}
			return null;
		}

		public ElementSet LayFamilyInstance(List<XYZ> xyzList, Reference face, FamilySymbol familySymbol, Autodesk.Revit.DB.Document doc)
		{
			ElementSet elementSet = new ElementSet();
			new List<FamilyInstanceCreationData>();
			XYZ xyz = new XYZ(1.0, 0.0, 0.0);
			foreach (XYZ xyz2 in xyzList)
			{
				UnifiedModified.ActivateFamilySymbol(familySymbol);
				FamilyInstance familyInstance = doc.Create.NewFamilyInstance(face, xyz2, xyz, familySymbol);
				if (familyInstance != null)
				{
					elementSet.Insert(familyInstance);
				}
			}
			return elementSet;
		}

		public ElementSet LayFamilyInstance(XYZ xyz, Reference face, FamilySymbol familySymbol, Autodesk.Revit.DB.Document doc)
		{
			ElementSet elementSet = new ElementSet();
			XYZ xyz2 = new XYZ(1.0, 0.0, 0.0);
			UnifiedModified.ActivateFamilySymbol(familySymbol);
			Element element = doc.Create.NewFamilyInstance(face, xyz, xyz2, familySymbol);
			if (element != null)
			{
				elementSet.Insert(element);
			}
			return elementSet;
		}

		public List<XYZ> CalculateLayFamilyInstanceXYZ(XYZ min, XYZ max, double rowDistance, double colDistance, double offSet)
		{
			List<XYZ> list = new List<XYZ>();
			if (min == null || max == null || rowDistance <= 0.0 || colDistance <= 0.0)
			{
				throw new Exception("布置参数有错误");
			}
			double num = UnitConvert.LengthToStd(rowDistance / 1000.0);
			double num2 = UnitConvert.LengthToStd(colDistance / 1000.0);
			if (Math.Abs(max.X - min.X) < num2 || Math.Abs(max.Y - min.Y) < num)
			{
				throw new Exception("选择区域过小");
			}
			double num3 = max.X - min.X;
			double num4 = max.Y - min.Y;
			int num5 = (int)Math.Ceiling(Math.Abs(num4 / num));
			int num6 = (int)Math.Ceiling(Math.Abs(num3 / num2));
			double num7 = (num3 - (double)(num6 - 1) * num2) / 2.0;
			double num8 = (num4 - (double)(num5 - 1) * num) / 2.0;
			double num9 = min.X;
			double num10 = min.Y;
			for (int i = 0; i < num5; i++)
			{
				if (i == 0)
				{
					num10 = min.Y + num8;
				}
				else
				{
					num10 = min.Y + num * (double)i + num8;
				}
				for (int j = 0; j < num6; j++)
				{
					if (j == 0)
					{
						num9 = min.X + num7;
					}
					else
					{
						num9 = min.X + num2 * (double)j + num7;
					}
					list.Add(new XYZ(num9, num10, offSet));
				}
			}
			return list;
		}

		public List<XYZ> CalculateLayFamilyInstanceForLingXingXYZ(XYZ min, XYZ max, int rowCount, int colCount, double ratio, double offSet, int N)
		{
			List<XYZ> list = new List<XYZ>();
			if (min == null || max == null || rowCount < 1 || colCount < 1 || ratio < 0.0)
			{
				return list;
			}
			double num = max.X - min.X;
			double num2 = max.Y - min.Y;
			double num3;
			double num4;
			if (colCount == 1)
			{
				num3 = num * ratio;
				num4 = 0.0;
			}
			else
			{
				num3 = num * ratio / ((double)(colCount - 1) + ratio);
				num4 = num / ((double)(colCount - 1) + ratio);
				if (N > 0)
				{
					double num5 = (double)N / 304.8;
					double num6 = num4 % num5;
					num4 -= num6;
					num3 = num - (double)(colCount - 1) * num4;
				}
			}
			double num7;
			double num8;
			if (rowCount == 1)
			{
				num7 = num2 * ratio;
				num8 = 0.0;
			}
			else
			{
				num7 = num2 * ratio / ((double)(rowCount - 1) + 2.0 * ratio);
				num8 = num2 / ((double)(rowCount - 1) + 2.0 * ratio);
				if (N > 0)
				{
					double num9 = (double)N / 304.8;
					double num10 = num8 % num9;
					num8 -= num10;
					num7 = (num2 - (double)(rowCount - 1) * num8) / 2.0;
				}
			}
			double num11 = min.X;
			double num12 = min.Y;
			for (int i = 0; i < rowCount; i++)
			{
				if (i == 0)
				{
					num12 = min.Y + num7;
				}
				else
				{
					num12 = min.Y + num8 * (double)i + num7;
				}
				for (int j = 0; j < colCount; j++)
				{
					if (i == 0 || i % 2 == 0)
					{
						num11 = min.X + num4 * (double)j + num3 / 4.0;
					}
					else
					{
						num11 = min.X + num4 * (double)j + num3 * 3.0 / 4.0;
					}
					list.Add(new XYZ(num11, num12, offSet));
				}
			}
			return list;
		}

		public List<XYZ> CalculateLayFamilyInstanceXYZ(XYZ min, XYZ max, int rowCount, int colCount, double ratio, double offSet, int N)
		{
			List<XYZ> list = new List<XYZ>();
			if (min == null || max == null || rowCount < 1 || colCount < 1 || ratio < 0.0)
			{
				throw new Exception("布置参数有错误");
			}
			double num = max.X - min.X;
			double num2 = max.Y - min.Y;
			double num3;
			double num4;
			if (colCount == 1)
			{
				num3 = num * ratio;
				num4 = 0.0;
			}
			else
			{
				num3 = num * ratio / ((double)(colCount - 1) + 2.0 * ratio);
				num4 = num / ((double)(colCount - 1) + 2.0 * ratio);
				if (N > 0)
				{
					double num5 = (double)N / 304.8;
					double num6 = num4 % num5;
					num4 -= num6;
					num3 = (num - (double)(colCount - 1) * num4) / 2.0;
				}
			}
			double num7;
			double num8;
			if (rowCount == 1)
			{
				num7 = num2 * ratio;
				num8 = 0.0;
			}
			else
			{
				num7 = num2 * ratio / ((double)(rowCount - 1) + 2.0 * ratio);
				num8 = num2 / ((double)(rowCount - 1) + 2.0 * ratio);
				if (N > 0)
				{
					double num9 = (double)N / 304.8;
					double num10 = num8 % num9;
					num8 -= num10;
					num7 = (num2 - (double)(rowCount - 1) * num8) / 2.0;
				}
			}
			double num11 = min.X;
			double num12 = min.Y;
			for (int i = 0; i < rowCount; i++)
			{
				if (i == 0)
				{
					num12 = min.Y + num7;
				}
				else
				{
					num12 = min.Y + num8 * (double)i + num7;
				}
				for (int j = 0; j < colCount; j++)
				{
					if (j == 0)
					{
						num11 = min.X + num3;
					}
					else
					{
						num11 = min.X + num4 * (double)j + num3;
					}
					list.Add(new XYZ(num11, num12, offSet));
				}
			}
			return list;
		}

		public List<XYZ> CalculateLayFamilyInstanceXYZ(XYZ min, XYZ max, int rowCount, int colCount, double ratio, double offSet)
		{
			List<XYZ> list = new List<XYZ>();
			if (min == null || max == null || rowCount < 1 || colCount < 1 || ratio < 0.0)
			{
				throw new Exception("布置参数有错误");
			}
			double num = max.X - min.X;
			double num2 = max.Y - min.Y;
			double num3;
			double num4;
			if (colCount == 1)
			{
				num3 = num * ratio;
				num4 = 0.0;
			}
			else
			{
				num3 = num * ratio / ((double)(colCount - 1) + 2.0 * ratio);
				num4 = num / ((double)(colCount - 1) + 2.0 * ratio);
			}
			double num5;
			double num6;
			if (rowCount == 1)
			{
				num5 = num2 * ratio;
				num6 = 0.0;
			}
			else
			{
				num5 = num2 * ratio / ((double)(rowCount - 1) + 2.0 * ratio);
				num6 = num2 / ((double)(rowCount - 1) + 2.0 * ratio);
			}
			double num7 = min.X;
			double num8 = min.Y;
			for (int i = 0; i < rowCount; i++)
			{
				if (i == 0)
				{
					num8 = min.Y + num5;
				}
				else
				{
					num8 = min.Y + num6 * (double)i + num5;
				}
				for (int j = 0; j < colCount; j++)
				{
					if (j == 0)
					{
						num7 = min.X + num3;
					}
					else
					{
						num7 = min.X + num4 * (double)j + num3;
					}
					list.Add(new XYZ(num7, num8, offSet));
				}
			}
			return list;
		}

		public List<XYZ> CalculateLayFamilyInstanceXYZ(XYZ pt1, XYZ pt2, XYZ pt3, int rowCount, int colCount, double ratio, double offSet)
		{
			List<XYZ> list = new List<XYZ>();
			if (pt1 == null || pt2 == null || pt3 == null || rowCount < 1 || colCount < 1 || ratio < 0.0)
			{
				throw new Exception("布置参数有错误");
			}
			XYZ xyz = (pt2 - pt1).Normalize();
			XYZ xyz2 = new XYZ(0.0, 0.0, 1.0).CrossProduct(xyz).Normalize();
			YJKLine hyline = new YJKLine(pt3, pt3 + 100.0 * xyz);
			hyline.MakeUnBound();
			pt3 = hyline.GetProject(pt2);
			double num = pt1.DistanceTo(pt2);
			double num2 = pt3.DistanceTo(pt2);
			if ((pt2 + xyz2 * num2 - pt2).AngleTo(pt3 - pt2) > Math.PI*.5)
			{
				xyz2 = -xyz2;
			}
			pt3 = pt2 + num2 * xyz2;
			double num3;
			double num4;
			if (colCount == 1)
			{
				num3 = num * ratio;
				num4 = 0.0;
			}
			else
			{
				num3 = num * ratio / ((double)(colCount - 1) + 2.0 * ratio);
				num4 = num / ((double)(colCount - 1) + 2.0 * ratio);
			}
			double num5;
			double num6;
			if (rowCount == 1)
			{
				num5 = num2 * ratio;
				num6 = 0.0;
			}
			else
			{
				num5 = num2 * ratio / ((double)(rowCount - 1) + 2.0 * ratio);
				num6 = num2 / ((double)(rowCount - 1) + 2.0 * ratio);
			}
			for (int i = 0; i < rowCount; i++)
			{
				XYZ xyz3;
				if (i == 0)
				{
					xyz3 = pt1 + num5 * xyz2;
				}
				else
				{
					xyz3 = pt1 + (num6 * (double)i + num5) * xyz2;
				}
				for (int j = 0; j < colCount; j++)
				{
					XYZ xyz4;
					if (j == 0)
					{
						xyz4 = xyz3 + num3 * xyz;
					}
					else
					{
						xyz4 = xyz3 + (num4 * (double)j + num3) * xyz;
					}
					list.Add(new XYZ(xyz4.X, xyz4.Y, offSet));
				}
			}
			return list;
		}

		public List<XYZ> CalculateLayFamilyInstanceXYZ(XYZ pt1, XYZ pt2, XYZ pt3, double colDistance, double rowDistance, double rowDistanceSide, double colDistanceSide, double offSet)
		{
			List<XYZ> list = new List<XYZ>();
			if (pt1 == null || pt2 == null || pt3 == null || rowDistance <= 0.0 || colDistance <= 0.0)
			{
				throw new Exception("布置参数有错误");
			}
			double num = UnitConvert.LengthToStd(rowDistance / 1000.0);
			double num2 = UnitConvert.LengthToStd(colDistance / 1000.0);
			double num3 = UnitConvert.LengthToStd(rowDistanceSide / 1000.0);
			double num4 = UnitConvert.LengthToStd(colDistanceSide / 1000.0);
			XYZ xyz = (pt2 - pt1).Normalize();
			XYZ xyz2 = new XYZ(0.0, 0.0, 1.0).CrossProduct(xyz).Normalize();
			YJKLine hyline = new YJKLine(pt3, pt3 + 100.0 * xyz);
			hyline.MakeUnBound();
			pt3 = hyline.GetProject(pt2);
			double num5 = pt1.DistanceTo(pt2);
			double num6 = pt3.DistanceTo(pt2);
			if ((pt2 + xyz2 * num6 - pt2).AngleTo(pt3 - pt2) > Math.PI*.5)
			{
				xyz2 = -xyz2;
			}
			pt3 = pt2 + num6 * xyz2;
			int num7;
			if (num2 <= 0.01)
			{
				num7 = 1;
			}
			else
			{
				num7 = Convert.ToInt32(Math.Floor((num6 - num4) / num2 + 1.0));
			}
			int num8;
			if (num <= 0.01)
			{
				num8 = 1;
			}
			else
			{
				num8 = Convert.ToInt32(Math.Floor((num5 - num3) / num + 1.0));
			}
			if (num8 < 1 || num7 < 1)
			{
				return null;
			}
			for (int i = 0; i < num8; i++)
			{
				XYZ xyz3;
				if (i == 0)
				{
					xyz3 = pt1 + num3 * xyz;
				}
				else
				{
					xyz3 = pt1 + (num * (double)i + num3) * xyz;
				}
				for (int j = 0; j < num7; j++)
				{
					XYZ xyz4;
					if (j == 0)
					{
						xyz4 = xyz3 + num4 * xyz2;
					}
					else
					{
						xyz4 = xyz3 + (num2 * (double)j + num4) * xyz2;
					}
					list.Add(new XYZ(xyz4.X, xyz4.Y, offSet));
				}
			}
			return list;
		}

		public List<XYZ> CalculateLayFamilyInstanceXYZ(XYZ min, XYZ max, double rowDistance, double colDistance, double rowDistanceSide, double colDistanceSide, double offSet)
		{
			List<XYZ> list = new List<XYZ>();
			if (min == null || max == null || rowDistance <= 0.0 || colDistance <= 0.0 || Math.Abs(min.Z - max.Z) > 0.001)
			{
				throw new Exception("布置参数有错误");
			}
			double num = UnitConvert.LengthToStd(rowDistance / 1000.0);
			double num2 = UnitConvert.LengthToStd(colDistance / 1000.0);
			double num3 = UnitConvert.LengthToStd(rowDistanceSide / 1000.0);
			double num4 = UnitConvert.LengthToStd(colDistanceSide / 1000.0);
			double num5 = Math.Abs(max.X - min.X);
			double num6 = Math.Abs(max.Y - min.Y);
			int num7;
			if (num2 <= 0.01)
			{
				num7 = 1;
			}
			else
			{
				num7 = Convert.ToInt32(Math.Floor((num5 - num4) / num2 + 1.0));
			}
			int num8;
			if (num <= 0.01)
			{
				num8 = 1;
			}
			else
			{
				num8 = Convert.ToInt32(Math.Floor((num6 - num3) / num + 1.0));
			}
			if (max.X - min.X < 0.0)
			{
				num2 = -num2;
				num4 = -num4;
			}
			if (max.Y - min.Y < 0.0)
			{
				num = -num;
				num3 = -num3;
			}
			double num9 = min.X;
			double num10 = min.Y;
			if (num8 < 1 || num7 < 1)
			{
				return null;
			}
			for (int i = 0; i < num8; i++)
			{
				if (i == 0)
				{
					num10 = min.Y + num3;
				}
				else
				{
					num10 = min.Y + num * (double)i + num3;
				}
				for (int j = 0; j < num7; j++)
				{
					if (j == 0)
					{
						num9 = min.X + num4;
					}
					else
					{
						num9 = min.X + num2 * (double)j + num4;
					}
					list.Add(new XYZ(num9, num10, offSet));
				}
			}
			return list;
		}

		public List<XYZ> CalculateLayFamilyInstanceXYZ(XYZ min, XYZ max, int rowCount, int colCount, double leftDistance, double rightDistance, double topDistance, double bottomDistance, double offSet)
		{
			new List<XYZ>();
			if (topDistance < 0.0 || bottomDistance < 0.0 || leftDistance < 0.0 || rightDistance < 0.0)
			{
				throw new Exception("布置参数有错误");
			}
			double num = UnitConvert.LengthToStd(leftDistance / 1000.0);
			double num2 = UnitConvert.LengthToStd(rightDistance / 1000.0);
			double num3 = UnitConvert.LengthToStd(topDistance / 1000.0);
			double num4 = UnitConvert.LengthToStd(bottomDistance / 1000.0);
			if (Math.Abs(max.X - min.X) - num - num2 < 0.0001 || Math.Abs(max.Y - min.Y) - num3 - num4 < 0.0001)
			{
				throw new Exception("选择区域过小");
			}
			XYZ max2;
			XYZ min2;
			if (max.X > min.X)
			{
				if (max.Y > min.Y)
				{
					max2 = new XYZ(max.X - num2, max.Y - num3, max.Z);
					min2 = new XYZ(min.X + num, min.Y + num4, min.Z);
				}
				else
				{
					max2 = new XYZ(max.X - num2, max.Y + num4, max.Z);
					min2 = new XYZ(min.X + num, min.Y - num3, min.Z);
				}
			}
			else if (max.Y > min.Y)
			{
				max2 = new XYZ(max.X + num, max.Y - num3, max.Z);
				min2 = new XYZ(min.X - num2, min.Y + num4, min.Z);
			}
			else
			{
				max2 = new XYZ(max.X + num, max.Y + num4, max.Z);
				min2 = new XYZ(min.X - num2, min.Y - num3, min.Z);
			}
			return this.CalculateLayFamilyInstanceXYZ(min2, max2, rowCount, colCount, 0.0, offSet);
		}

		public List<XYZ> CalculateLaySprinklersXYZ(XYZ min, XYZ max, double rowDistance, double colDistance, double rowDistanceSide, double colDistanceSide, double lessDistance, double offSet, int N)
		{
			List<XYZ> list = new List<XYZ>();
			double num = rowDistance;
			double num2 = colDistance;
			double num3 = rowDistanceSide;
			double num4 = colDistanceSide;
			double num5 = (double)((int)(Math.Abs(min.Y - max.Y) * 304.8));
			double num6 = (double)((int)(Math.Abs(min.X - max.X) * 304.8));
			int num7 = (int)Math.Ceiling((num5 - num3 * 2.0) / num3) + 1;
			int num8 = (int)Math.Ceiling((num6 - num4 * 2.0) / num4) + 1;
			if (num7 <= 0 || num8 <= 0)
			{
				throw new Exception("参数设置错误，可能是边距设置过大或过小");
			}
			bool flag = false;
			bool flag2 = false;
			if (N > 0)
			{
				int num9 = (int)rowDistance;
				while ((double)num9 >= lessDistance)
				{
					if ((num5 - num3 * 2.0) % (double)num9 == 0.0)
					{
						num7 = (int)Math.Ceiling((num5 - num3 * 2.0) / (double)num9) + 1;
						flag = true;
						num = (double)num9;
						break;
					}
					if ((num5 - num3 * 2.0) % (double)num9 > 0.0)
					{
						int num10 = (int)Math.Ceiling((num5 - num3 * 2.0) / (double)num9);
						if (num5 - (double)(num10 * num9) > 0.0 && (num5 - (double)(num10 * num9)) / 2.0 <= num3 && (num5 - (double)(num10 * num9)) / 2.0 >= 100.0)
						{
							flag = true;
							num = (double)num9;
							num3 = (num5 - (double)(num10 * num9)) / 2.0;
							num7 = num10 + 1;
							int num11 = num9 - N;
							while ((double)num11 >= lessDistance)
							{
								if ((double)(num10 * N / 2) + num3 > rowDistanceSide)
								{
									break;
								}
								num3 = (double)(num10 * N / 2) + num3;
								num = (double)num11;
								num11 -= N;
							}
							break;
						}
					}
					num9 -= N;
				}
			}
			else if ((num5 - num3 * 2.0) % rowDistance == 0.0)
			{
				num7 = (int)Math.Ceiling((num5 - num3 * 2.0) / rowDistance) + 1;
				flag = true;
				num = rowDistance;
			}
			else
			{
				int num12 = (int)Math.Ceiling((num5 - num3 * 2.0) / rowDistance);
				flag = true;
				num3 = rowDistanceSide;
				num7 = num12 + 1;
				num = (num5 - num3 * 2.0) / (double)num12;
			}
			if (N > 0)
			{
				int num13 = (int)colDistance;
				while ((double)num13 >= lessDistance)
				{
					if ((num6 - num4 * 2.0) % (double)num13 == 0.0)
					{
						num8 = (int)Math.Ceiling((num6 - num4 * 2.0) / (double)num13) + 1;
						flag2 = true;
						num2 = (double)num13;
						break;
					}
					if ((num6 - num4 * 2.0) % (double)num13 > 0.0)
					{
						int num14 = (int)Math.Ceiling((num6 - num4 * 2.0) / (double)num13);
						if (num6 - (double)(num14 * num13) > 0.0 && (num6 - (double)(num14 * num13)) / 2.0 <= num4 && (num6 - (double)(num14 * num13)) / 2.0 >= 100.0)
						{
							flag2 = true;
							num2 = (double)num13;
							num4 = (num6 - (double)(num14 * num13)) / 2.0;
							num8 = num14 + 1;
							if (N <= 0)
							{
								N = 1;
							}
							if (N > 0)
							{
								int num15 = num13 - N;
								while ((double)num15 >= lessDistance)
								{
									if ((double)(num14 * N / 2) + num4 > colDistanceSide)
									{
										break;
									}
									num4 = (double)(num14 * N / 2) + num4;
									num2 = (double)num15;
									num15 -= N;
								}
								break;
							}
							break;
						}
					}
					num13 -= N;
				}
			}
			else if ((num6 - num4 * 2.0) % colDistance == 0.0)
			{
				num8 = (int)Math.Ceiling((num6 - num4 * 2.0) / colDistance) + 1;
				flag2 = true;
				num2 = colDistance;
			}
			else
			{
				int num16 = (int)Math.Ceiling((num6 - num4 * 2.0) / colDistance);
				flag2 = true;
				num4 = colDistanceSide;
				num8 = num16 + 1;
				num2 = (num6 - num4 * 2.0) / (double)num16;
			}
			if (flag && flag2)
			{
				double num17 = min.X;
				double num18 = min.Y;
				if (max.X - min.X < 0.0)
				{
					num2 = -num2;
					num4 = -num4;
				}
				if (max.Y - min.Y < 0.0)
				{
					num = -num;
					num3 = -num3;
				}
				num /= 304.8;
				num2 /= 304.8;
				num3 /= 304.8;
				num4 /= 304.8;
				for (int i = 0; i < num7; i++)
				{
					if (i == 0)
					{
						num18 = min.Y + num3;
					}
					else
					{
						num18 = min.Y + num * (double)i + num3;
					}
					for (int j = 0; j < num8; j++)
					{
						if (j == 0)
						{
							num17 = min.X + num4;
						}
						else
						{
							num17 = min.X + num2 * (double)j + num4;
						}
						list.Add(new XYZ(num17, num18, offSet));
					}
				}
				return list;
			}
			throw new Exception("参数设置错误，可能是边距设置过大或过小");
		}

		public List<XYZ> CalculateVerticalLaySprinklers(XYZ min, XYZ max, XYZ end, double rowDistance, double rowDistanceSide, double lessDistance, double offSet, int N)
		{
			List<XYZ> list = new List<XYZ>();
			double num = rowDistance;
			double num2 = rowDistanceSide;
			new YJKLine(min, max).MakeUnBound();
			min.DistanceTo(max);
			double num3 = (double)((int)(min.DistanceTo(max) * 304.8));
			int num4 = (int)Math.Ceiling((num3 - num2 * 2.0) / num2) + 1;
			XYZ xyz = new XYZ();
			if (max.Y - min.Y > 0.0)
			{
				xyz = new XYZ(0.0, 1.0, 0.0);
			}
			else
			{
				xyz = new XYZ(0.0, -1.0, 0.0);
			}
			XYZ xyz2 = new XYZ(0.0, 0.0, 1.0).CrossProduct(xyz).Normalize();
			if ((max + xyz2 * num3 - max).AngleTo(end - max) > Math.PI*.5)
			{
				xyz2 = -xyz2;
			}
			if (N > 0)
			{
				int num5 = (int)rowDistance;
				while ((double)num5 >= lessDistance)
				{
					if ((num3 - num2 * 2.0) % (double)num5 == 0.0)
					{
						num4 = (int)Math.Ceiling((num3 - num2 * 2.0) / (double)num5) + 1;
						num = (double)num5;
						break;
					}
					if ((num3 - num2 * 2.0) % (double)num5 > 0.0)
					{
						int num6 = (int)Math.Ceiling((num3 - num2 * 2.0) / (double)num5);
						if (num3 - (double)(num6 * num5) > 0.0 && (num3 - (double)(num6 * num5)) / 2.0 <= num2 && (num3 - (double)(num6 * num5)) / 2.0 >= 100.0)
						{
							num = (double)num5;
							num2 = (num3 - (double)(num6 * num5)) / 2.0;
							num4 = num6 + 1;
							int num7 = num5 - N;
							while ((double)num7 >= lessDistance)
							{
								if ((double)(num6 * N / 2) + num2 > rowDistanceSide)
								{
									break;
								}
								num2 = (double)(num6 * N / 2) + num2;
								num = (double)num7;
								num7 -= N;
							}
							break;
						}
					}
					num5 -= N;
				}
			}
			else if ((num3 - num2 * 2.0) % rowDistance == 0.0)
			{
				num4 = (int)Math.Ceiling((num3 - num2 * 2.0) / rowDistance) + 1;
				num = rowDistance;
			}
			else
			{
				int num8 = (int)Math.Ceiling((num3 - num2 * 2.0) / rowDistance);
				num2 = rowDistanceSide;
				num4 = num8 + 1;
				num = (num3 - num2 * 2.0) / (double)num8;
			}
			this.AdjustmentDistance(ref num2, N);
			if (num4 == 1)
			{
				num2 = num3 / 2.0;
			}
			for (int i = 0; i < num4; i++)
			{
				XYZ xyz3;
				if (i == 0)
				{
					xyz3 = min + num2 / 304.8 * xyz;
				}
				else
				{
					xyz3 = min + (num / 304.8 * (double)i + num2 / 304.8) * xyz;
				}
				list.Add(new XYZ(xyz3.X, xyz3.Y, offSet));
			}
			return list;
		}

		public List<XYZ> CalculateWidthLessLaySprinklers(XYZ min, XYZ max, XYZ end, double rowDistance, double colDistance, double rowDistanceSide, double colDistanceSide, double lessDistance, double offSet, int N, int type)
		{
			List<XYZ> list = new List<XYZ>();
			double num = rowDistance;
			double num2 = colDistance;
			double num3 = rowDistanceSide;
			double num4 = colDistanceSide;
			XYZ xyz = new XYZ();
			double num5 = (double)((int)(max.DistanceTo(min) * 304.8));
			double num6 = (double)((int)(max.DistanceTo(end) * 304.8));
			if (type == 0)
			{
				if (end.Y - min.Y > 0.0)
				{
					xyz = new XYZ(0.0, 1.0, 0.0);
				}
				else
				{
					xyz = new XYZ(0.0, -1.0, 0.0);
				}
				int num7 = (int)Math.Ceiling((num6 - num3 * 2.0) / num3) + 1;
				if (N > 0)
				{
					int num8 = (int)rowDistance;
					while ((double)num8 >= lessDistance)
					{
						if ((num6 - num3 * 2.0) % (double)num8 == 0.0)
						{
							num7 = (int)Math.Ceiling((num6 - num3 * 2.0) / (double)num8) + 1;
							num = (double)num8;
							break;
						}
						if ((num6 - num3 * 2.0) % (double)num8 > 0.0)
						{
							int num9 = (int)Math.Ceiling((num6 - num3 * 2.0) / (double)num8);
							if (num6 - (double)(num9 * num8) > 0.0 && (num6 - (double)(num9 * num8)) / 2.0 <= num3 && (num6 - (double)(num9 * num8)) / 2.0 >= 100.0)
							{
								num = (double)num8;
								num3 = (num6 - (double)(num9 * num8)) / 2.0;
								num7 = num9 + 1;
								int num10 = num8 - N;
								while ((double)num10 >= lessDistance)
								{
									if ((double)(num9 * N / 2) + num3 > rowDistanceSide)
									{
										break;
									}
									num3 = (double)(num9 * N / 2) + num3;
									num = (double)num10;
									num10 -= N;
								}
								break;
							}
						}
						num8 -= N;
					}
				}
				else if ((num6 - num3 * 2.0) % rowDistance == 0.0)
				{
					num7 = (int)Math.Ceiling((num6 - num3 * 2.0) / rowDistance) + 1;
					num = rowDistance;
				}
				else
				{
					int num11 = (int)Math.Ceiling((num6 - num3 * 2.0) / rowDistance);
					num3 = rowDistanceSide;
					num7 = num11 + 1;
					num = (num6 - num3 * 2.0) / (double)num11;
				}
				this.AdjustmentDistance(ref num3, N);
				XYZ xyz2 = (min + max) / 2.0;
				for (int i = 0; i < num7; i++)
				{
					XYZ xyz3;
					if (i == 0)
					{
						xyz3 = xyz2 + num3 / 304.8 * xyz;
					}
					else
					{
						xyz3 = xyz2 + (num / 304.8 * (double)i + num3 / 304.8) * xyz;
					}
					list.Add(new XYZ(xyz3.X, xyz3.Y, offSet));
				}
			}
			if (type == 1)
			{
				if (end.X - min.X > 0.0)
				{
					xyz = new XYZ(1.0, 0.0, 0.0);
				}
				else
				{
					xyz = new XYZ(-1.0, 0.0, 0.0);
				}
				int num12 = (int)Math.Ceiling((num5 - num4 * 2.0) / num4) + 1;
				if (N > 0)
				{
					int num13 = (int)colDistance;
					while ((double)num13 >= lessDistance)
					{
						if ((num5 - num4 * 2.0) % (double)num13 == 0.0)
						{
							num12 = (int)Math.Ceiling((num5 - num4 * 2.0) / (double)num13) + 1;
							num2 = (double)num13;
							break;
						}
						if ((num5 - num4 * 2.0) % (double)num13 > 0.0)
						{
							int num14 = (int)Math.Ceiling((num5 - num4 * 2.0) / (double)num13);
							if (num5 - (double)(num14 * num13) > 0.0 && (num5 - (double)(num14 * num13)) / 2.0 <= num4 && (num5 - (double)(num14 * num13)) / 2.0 >= 100.0)
							{
								num2 = (double)num13;
								num4 = (num5 - (double)(num14 * num13)) / 2.0;
								num12 = num14 + 1;
								if (N <= 0)
								{
									N = 1;
								}
								if (N > 0)
								{
									int num15 = num13 - N;
									while ((double)num15 >= lessDistance)
									{
										if ((double)(num14 * N / 2) + num4 > colDistanceSide)
										{
											break;
										}
										num4 = (double)(num14 * N / 2) + num4;
										num2 = (double)num15;
										num15 -= N;
									}
									break;
								}
								break;
							}
						}
						num13 -= N;
					}
				}
				else if ((num5 - num4 * 2.0) % colDistance == 0.0)
				{
					num12 = (int)Math.Ceiling((num5 - num4 * 2.0) / colDistance) + 1;
					num2 = colDistance;
				}
				else
				{
					int num16 = (int)Math.Ceiling((num5 - num4 * 2.0) / colDistance);
					num4 = colDistanceSide;
					num12 = num16 + 1;
					num2 = (num5 - num4 * 2.0) / (double)num16;
				}
				this.AdjustmentDistance(ref num4, N);
				XYZ xyz4 = new XYZ(min.X, end.Y, end.Z);
				XYZ xyz5 = (min + xyz4) / 2.0;
				for (int j = 0; j < num12; j++)
				{
					XYZ xyz6;
					if (j == 0)
					{
						xyz6 = xyz5 + num4 / 304.8 * xyz;
					}
					else
					{
						xyz6 = xyz5 + (num2 / 304.8 * (double)j + num4 / 304.8) * xyz;
					}
					list.Add(new XYZ(xyz6.X, xyz6.Y, offSet));
				}
			}
			return list;
		}

		public List<XYZ> CalculateLaySprinklersXYZ(XYZ min, XYZ max, XYZ end, double rowDistance, double colDistance, double rowDistanceSide, double colDistanceSide, double lessDistance, double offSet, int N, bool bl)
		{
			List<XYZ> list = new List<XYZ>();
			double num = rowDistance;
			double num2 = colDistance;
			double num3 = rowDistanceSide;
			double num4 = colDistanceSide;
			YJKLine hyline = new YJKLine(min, max);
			hyline.MakeUnBound();
			double num5 = (double)((int)(min.DistanceTo(max) * 304.8));
			double num6 = (double)((int)(hyline.GetProject(end).DistanceTo(end) * 304.8));
			int num7 = (int)Math.Ceiling((num6 - num3 * 2.0) / num3) + 1;
			int num8 = (int)Math.Ceiling((num5 - num4 * 2.0) / num4) + 1;
			XYZ xyz = (max - min).Normalize();
			XYZ xyz2 = new XYZ(0.0, 0.0, 1.0).CrossProduct(xyz).Normalize();
			if ((max + xyz2 * num6 - max).AngleTo(end - max) > Math.PI*.5)
			{
				xyz2 = -xyz2;
			}
			if (N > 0)
			{
				int num9 = (int)rowDistance;
				while ((double)num9 >= lessDistance)
				{
					if ((num6 - num3 * 2.0) % (double)num9 == 0.0)
					{
						num7 = (int)Math.Ceiling((num6 - num3 * 2.0) / (double)num9) + 1;
						num = (double)num9;
						break;
					}
					if ((num6 - num3 * 2.0) % (double)num9 > 0.0)
					{
						int num10 = (int)Math.Ceiling((num6 - num3 * 2.0) / (double)num9);
						if (num6 - (double)(num10 * num9) > 0.0 && (num6 - (double)(num10 * num9)) / 2.0 <= num3 && (num6 - (double)(num10 * num9)) / 2.0 >= 100.0)
						{
							num = (double)num9;
							num3 = (num6 - (double)(num10 * num9)) / 2.0;
							num7 = num10 + 1;
							int num11 = num9 - N;
							while ((double)num11 >= lessDistance)
							{
								if ((double)(num10 * N / 2) + num3 > rowDistanceSide)
								{
									break;
								}
								num3 = (double)(num10 * N / 2) + num3;
								num = (double)num11;
								num11 -= N;
							}
							break;
						}
					}
					num9 -= N;
				}
			}
			else if ((num6 - num3 * 2.0) % rowDistance == 0.0)
			{
				num7 = (int)Math.Ceiling((num6 - num3 * 2.0) / rowDistance) + 1;
				num = rowDistance;
			}
			else
			{
				int num12 = (int)Math.Ceiling((num6 - num3 * 2.0) / rowDistance);
				num3 = rowDistanceSide;
				num7 = num12 + 1;
				num = (num6 - num3 * 2.0) / (double)num12;
			}
			if (N > 0)
			{
				int num13 = (int)colDistance;
				while ((double)num13 >= lessDistance)
				{
					if ((num5 - num4 * 2.0) % (double)num13 == 0.0)
					{
						num8 = (int)Math.Ceiling((num5 - num4 * 2.0) / (double)num13) + 1;
						num2 = (double)num13;
						break;
					}
					if ((num5 - num4 * 2.0) % (double)num13 > 0.0)
					{
						int num14 = (int)Math.Ceiling((num5 - num4 * 2.0) / (double)num13);
						if (num5 - (double)(num14 * num13) > 0.0 && (num5 - (double)(num14 * num13)) / 2.0 <= num4 && (num5 - (double)(num14 * num13)) / 2.0 >= 100.0)
						{
							num2 = (double)num13;
							num4 = (num5 - (double)(num14 * num13)) / 2.0;
							num8 = num14 + 1;
							if (N <= 0)
							{
								N = 1;
							}
							if (N > 0)
							{
								int num15 = num13 - N;
								while ((double)num15 >= lessDistance)
								{
									if ((double)(num14 * N / 2) + num4 > colDistanceSide)
									{
										break;
									}
									num4 = (double)(num14 * N / 2) + num4;
									num2 = (double)num15;
									num15 -= N;
								}
								break;
							}
							break;
						}
					}
					num13 -= N;
				}
			}
			else if ((num5 - num4 * 2.0) % colDistance == 0.0)
			{
				num8 = (int)Math.Ceiling((num5 - num4 * 2.0) / colDistance) + 1;
				num2 = colDistance;
			}
			else
			{
				int num16 = (int)Math.Ceiling((num5 - num4 * 2.0) / colDistance);
				num4 = colDistanceSide;
				num8 = num16 + 1;
				num2 = (num5 - num4 * 2.0) / (double)num16;
			}
			this.AdjustmentDistance(ref num3, N);
			this.AdjustmentDistance(ref num4, N);
			if (num7 == 1)
			{
				num3 = num6 / 2.0;
			}
			if (num8 == 1)
			{
				num4 = num5 / 2.0;
			}
			if (bl)
			{
				if (num7 == 0)
				{
					for (int i = 0; i < num8; i++)
					{
						XYZ xyz3;
						if (i == 0)
						{
							xyz3 = min + num4 / 304.8 * xyz;
						}
						else
						{
							xyz3 = min + (num2 / 304.8 * (double)i + num4 / 304.8) * xyz;
						}
						list.Add(new XYZ(xyz3.X, xyz3.Y, offSet));
					}
				}
				if (num8 == 0)
				{
					for (int j = 0; j < num7; j++)
					{
						XYZ xyz4;
						if (j == 0)
						{
							xyz4 = min + num3 / 304.8 * xyz;
						}
						else
						{
							xyz4 = min + (num / 304.8 * (double)j + num3 / 304.8) * xyz;
						}
						list.Add(new XYZ(xyz4.X, xyz4.Y, offSet));
					}
				}
			}
			for (int k = 0; k < num7; k++)
			{
				XYZ xyz5;
				if (k == 0)
				{
					xyz5 = min + num3 / 304.8 * xyz2;
				}
				else
				{
					xyz5 = min + (num / 304.8 * (double)k + num3 / 304.8) * xyz2;
				}
				for (int l = 0; l < num8; l++)
				{
					XYZ xyz6;
					if (l == 0)
					{
						xyz6 = xyz5 + num4 / 304.8 * xyz;
					}
					else
					{
						xyz6 = xyz5 + (num2 / 304.8 * (double)l + num4 / 304.8) * xyz;
					}
					list.Add(new XYZ(xyz6.X, xyz6.Y, offSet));
				}
			}
			return list;
		}

		private void AdjustmentDistance(ref double dValue, int n)
		{
			int num = (int)dValue;
			int num2 = num % n;
			if (num2 > n / 2)
			{
				num += n - num2;
			}
			else
			{
				num -= num2;
			}
			dValue = (double)num;
		}

		public List<XYZ> CalculateLaySprinklersForLingXingXYZ(XYZ min, XYZ max, double rowDistance, double colDistance, double rowDistanceSide, double colDistanceSide, double lessDistance, double offSet, int N)
		{
			List<XYZ> list = new List<XYZ>();
			double num = rowDistance;
			double num2 = colDistance;
			double num3 = rowDistanceSide;
			double num4 = colDistanceSide * 4.0 / 3.0;
			double num5 = (double)((int)(Math.Abs(min.Y - max.Y) * 304.8));
			double num6 = (double)((int)(Math.Abs(min.X - max.X) * 304.8));
			int num7 = (int)Math.Ceiling((num5 - num3 * 2.0) / rowDistance) + 1;
			int num8 = (int)Math.Ceiling((num6 - num4) / colDistance) + 1;
			if (num7 <= 0 || num8 <= 0)
			{
				return list;
			}
			bool flag = false;
			bool flag2 = false;
			if (N > 0)
			{
				int num9 = (int)rowDistance;
				while ((double)num9 >= lessDistance)
				{
					if ((num5 - num3 * 2.0) % (double)num9 == 0.0)
					{
						num7 = (int)Math.Ceiling((num5 - num3 * 2.0) / (double)num9) + 1;
						flag = true;
						num = (double)num9;
						break;
					}
					if ((num5 - num3 * 2.0) % (double)num9 > 0.0)
					{
						int num10 = (int)Math.Ceiling((num5 - num3 * 2.0) / (double)num9);
						if (num5 - (double)(num10 * num9) > 0.0 && (num5 - (double)(num10 * num9)) / 2.0 <= num3 && (num5 - (double)(num10 * num9)) / 2.0 >= 100.0)
						{
							flag = true;
							num = (double)num9;
							num3 = (num5 - (double)(num10 * num9)) / 2.0;
							num7 = num10 + 1;
							int num11 = num9 - N;
							while ((double)num11 >= lessDistance)
							{
								if ((double)(num10 * N / 2) + num3 > rowDistanceSide)
								{
									break;
								}
								num3 = (double)(num10 * N / 2) + num3;
								num = (double)num11;
								num11 -= N;
							}
							break;
						}
					}
					num9 -= N;
				}
			}
			else if ((num5 - num3 * 2.0) % rowDistance == 0.0)
			{
				num7 = (int)Math.Ceiling((num5 - num3 * 2.0) / rowDistance) + 1;
				flag = true;
				num = rowDistance;
			}
			else
			{
				int num12 = (int)Math.Ceiling((num5 - num3 * 2.0) / rowDistance);
				flag = true;
				num3 = rowDistanceSide;
				num7 = num12 + 1;
				num = (num5 - num3 * 2.0) / (double)num12;
			}
			if (N > 0)
			{
				int num13 = (int)colDistance;
				while ((double)num13 >= lessDistance)
				{
					if ((num6 - num4) % (double)num13 == 0.0)
					{
						num8 = (int)Math.Ceiling((num6 - num4) / (double)num13) + 1;
						flag2 = true;
						num2 = (double)num13;
						break;
					}
					if ((num6 - num4) % (double)num13 > 0.0)
					{
						int num14 = (int)Math.Ceiling((num6 - num4) / (double)num13);
						if (num6 - (double)(num14 * num13) <= num4 && (num6 - (double)(num14 * num13)) / 4.0 >= 100.0)
						{
							flag2 = true;
							num2 = (double)num13;
							num4 = num6 - (double)(num14 * num13);
							num8 = num14 + 1;
							int num15 = num13 - N;
							while ((double)num15 >= lessDistance)
							{
								if ((double)(num14 * N) + num4 > colDistanceSide * 4.0 / 3.0)
								{
									break;
								}
								num4 = (double)(num14 * N) + num4;
								num2 = (double)num15;
								num15 -= N;
							}
							break;
						}
					}
					num13 -= N;
				}
			}
			else if ((num6 - num4) % colDistance == 0.0)
			{
				num8 = (int)Math.Ceiling((num6 - num4) / colDistance) + 1;
				flag2 = true;
				num2 = colDistance;
			}
			else
			{
				int num16 = (int)Math.Ceiling((num6 - num4) / colDistance);
				flag2 = true;
				num2 = (num6 - num4) / (double)num16;
				num8 = num16 + 1;
			}
			if (flag && flag2)
			{
				double num17 = min.X;
				double num18 = min.Y;
				if (max.X - min.X < 0.0)
				{
					num2 = -num2;
					num4 = -num4;
				}
				if (max.Y - min.Y < 0.0)
				{
					num = -num;
					num3 = -num3;
				}
				num /= 304.8;
				num2 /= 304.8;
				this.AdjustmentDistance(ref num3, N);
				this.AdjustmentDistance(ref num4, N);
				num3 /= 304.8;
				num4 /= 304.8;
				for (int i = 0; i < num7; i++)
				{
					if (i == 0)
					{
						num18 = min.Y + num3;
					}
					else
					{
						num18 = min.Y + num * (double)i + num3;
					}
					for (int j = 0; j < num8; j++)
					{
						if (i == 0 || i % 2 == 0)
						{
							num17 = min.X + num2 * (double)j + num4 / 4.0;
						}
						else
						{
							num17 = min.X + num2 * (double)j + num4 * 3.0 / 4.0;
						}
						list.Add(new XYZ(num17, num18, offSet));
					}
				}
			}
			return list;
		}

		public XYZ Project(XYZ point, Curve curve, double deviation = 0.0001)
		{
			IntersectionResult intersectionResult = new IntersectionResult();
			intersectionResult = curve.Project(point);
			if (intersectionResult == null || intersectionResult.XYZPoint == null)
			{
				return null;
			}
			if (curve.IsBound && (curve.GetEndPoint(0).DistanceTo(intersectionResult.XYZPoint) < deviation || curve.GetEndPoint(1).DistanceTo(intersectionResult.XYZPoint) < deviation))
			{
				return null;
			}
			return intersectionResult.XYZPoint;
		}

		public IList<AutoConnectParameter> InitYJKElement(Duct duct, IList<Element> listElement)
		{
			IList<AutoConnectParameter> list = new List<AutoConnectParameter>();
			Connector connector = this.GetDuctConnectorEnd(duct)[0];
			foreach (Element element in listElement)
			{
				AutoConnectParameter autoConnectParameter = new AutoConnectParameter();
				autoConnectParameter.ElementId = element.Id;
				autoConnectParameter.Connector = this.GetYJKElementConnector(element);
				autoConnectParameter.LocationPoint = this.GetLocationPoint(element);
				autoConnectParameter.PointOnDuct = this.GetProjectOnDuct(duct, element, 0.33);
				autoConnectParameter.PointOnFace = new XYZ(autoConnectParameter.LocationPoint.Point.X, autoConnectParameter.LocationPoint.Point.Y, connector.Origin.Z);
				if (autoConnectParameter.PointOnDuct != null)
				{
					autoConnectParameter.VectorToDuct = autoConnectParameter.PointOnDuct - autoConnectParameter.PointOnFace;
				}
				list.Add(autoConnectParameter);
			}
			return list;
		}

		public IList<AutoConnectParameter> InitYJKElement(Pipe pipe, IList<Element> listElement)
		{
			IList<AutoConnectParameter> list = new List<AutoConnectParameter>();
			Connector connector = this.GetPipeConnectorEnd(pipe)[0];
			foreach (Element element in listElement)
			{
				AutoConnectParameter autoConnectParameter = new AutoConnectParameter();
				autoConnectParameter.ElementId = element.Id;
				autoConnectParameter.Connector = this.GetYJKElementConnector(element);
				autoConnectParameter.LocationPoint = this.GetLocationPoint(element);
				autoConnectParameter.PointOnDuct = this.GetProjectOnPipe(pipe, element, 0.03);
				autoConnectParameter.PointOnFace = new XYZ(autoConnectParameter.LocationPoint.Point.X, autoConnectParameter.LocationPoint.Point.Y, connector.Origin.Z);
				if (autoConnectParameter.PointOnDuct != null)
				{
					autoConnectParameter.VectorToDuct = autoConnectParameter.PointOnDuct - autoConnectParameter.PointOnFace;
				}
				list.Add(autoConnectParameter);
			}
			return list;
		}

		public IList<AutoConnectParameter> InitYJKElement(Pipe pipe, IList<Element> listElement, double pinoutLength)
		{
			IList<AutoConnectParameter> list = new List<AutoConnectParameter>();
			Connector connector = this.GetPipeConnectorEnd(pipe)[0];
			foreach (Element element in listElement)
			{
				AutoConnectParameter autoConnectParameter = new AutoConnectParameter();
				Connector matchedConnector = this.GetMatchedConnector(connector, element as FamilyInstance);
				if (matchedConnector != null)
				{
					autoConnectParameter.ElementId = element.Id;
					autoConnectParameter.Connector = matchedConnector;
					autoConnectParameter.LocationPoint = this.GetLocationPoint(element);
					autoConnectParameter.PointOnDuct = this.GetProjectOnPipe(pipe, matchedConnector.Origin);
					autoConnectParameter.PointOnFace = new XYZ(matchedConnector.Origin.X, matchedConnector.Origin.Y, connector.Origin.Z);
					if (autoConnectParameter.PointOnDuct != null)
					{
						autoConnectParameter.VectorToDuct = autoConnectParameter.PointOnDuct - autoConnectParameter.PointOnFace;
					}
					autoConnectParameter.ExtendPoint = matchedConnector.CoordinateSystem.BasisZ * pinoutLength + matchedConnector.Origin;
					autoConnectParameter.ExtendPointOnDuct = this.GetProjectOnPipe(pipe, autoConnectParameter.ExtendPoint);
					autoConnectParameter.ReDirection = this.RelativeDirectionOfPipeAndConn(pipe, matchedConnector);
					list.Add(autoConnectParameter);
				}
			}
			return list;
		}

		public IList<FanCoilParameter> InitYJKElement(Pipe pipe, double pinoutLength, IList<Element> listElement)
		{
			IList<FanCoilParameter> list = new List<FanCoilParameter>();
			Connector pipeConn = this.GetPipeConnectorEnd(pipe)[0];
			foreach (Element element in listElement)
			{
				FanCoilParameter fanCoilParameter = new FanCoilParameter();
				Connector matchedConnector = this.GetMatchedConnector(pipeConn, element as FamilyInstance);
				if (matchedConnector != null && !matchedConnector.IsConnected)
				{
					fanCoilParameter.ElementId = element.Id;
					fanCoilParameter.Connector = matchedConnector;
					fanCoilParameter.ConnPoint = matchedConnector.Origin;
					fanCoilParameter.ExtendPoint = matchedConnector.CoordinateSystem.BasisZ * pinoutLength + matchedConnector.Origin;
					fanCoilParameter.PointOnPipe = this.GetProjectOnPipe(pipe, fanCoilParameter.ExtendPoint);
					fanCoilParameter.PointOnFace = new XYZ(fanCoilParameter.PointOnPipe.X, fanCoilParameter.PointOnPipe.Y, matchedConnector.Origin.Z);
					fanCoilParameter.HorizontalVector = fanCoilParameter.PointOnFace.Subtract(fanCoilParameter.ExtendPoint);
					fanCoilParameter.VerticalVector = fanCoilParameter.PointOnPipe.Subtract(fanCoilParameter.PointOnFace);
					fanCoilParameter.ReDirection = this.RelativeDirectionOfPipeAndConn(pipe, matchedConnector);
					list.Add(fanCoilParameter);
				}
			}
			return list;
		}

		public IList<AutoConnectParameter> InitHYPipe(Pipe pipe, IList<Element> listElement)
		{
			IList<AutoConnectParameter> list = new List<AutoConnectParameter>();
			Connector connector = this.GetPipeConnectorEnd(pipe)[0];
			foreach (Element element in listElement)
			{
				AutoConnectParameter autoConnectParameter = new AutoConnectParameter();
				autoConnectParameter.ElementId = element.Id;
				ConnectorSetIterator connectorSetIterator = (element as Pipe).ConnectorManager.Connectors.ForwardIterator();
				if (connectorSetIterator.MoveNext())
				{
					Connector connector2 = connectorSetIterator.Current as Connector;
					autoConnectParameter.Connector = connector2;
				}
				YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(element as Pipe);
				autoConnectParameter.PipePoint = (hypiping.StartConnector.Origin + hypiping.EndConnector.Origin) / 2.0;
				LocationCurve locationCurve = pipe.Location as LocationCurve;
				autoConnectParameter.PointOnDuct = this.Project(autoConnectParameter.Connector.Origin, locationCurve.Curve, 0.03);
				autoConnectParameter.PointOnFace = new XYZ(autoConnectParameter.Connector.Origin.X, autoConnectParameter.Connector.Origin.Y, connector.Origin.Z);
				if (autoConnectParameter.PointOnDuct != null)
				{
					autoConnectParameter.VectorToDuct = autoConnectParameter.PointOnDuct - autoConnectParameter.PointOnFace;
				}
				list.Add(autoConnectParameter);
			}
			return list;
		}

		public XYZ GetProjectOnDuct(Duct duct, Element elem, double deviation = 0.0001)
		{
			LocationCurve locationCurve = duct.Location as LocationCurve;
			LocationPoint locationPoint = this.GetLocationPoint(elem);
			return this.Project(locationPoint.Point, locationCurve.Curve, deviation);
		}

		public XYZ GetProjectOnPipe(Pipe pipe, XYZ point, double deviation = 0.001)
		{
			LocationCurve locationCurve = pipe.Location as LocationCurve;
			return this.Project(point, locationCurve.Curve, deviation);
		}

		public Connector GetMatchedConnector(Connector pipeConn, FamilyInstance fi)
		{
			Connector result = null;
			foreach (Connector connector in this.GetYJKElementConnector(fi, (Autodesk.Revit.DB.Domain)3))
			{
				if ((connector.PipeSystemType == pipeConn.PipeSystemType || (connector.PipeSystemType == 22 && pipeConn.PipeSystemType != 7 && pipeConn.PipeSystemType != 8)) && !connector.IsConnected)
				{
					result = connector;
					break;
				}
			}
			return result;
		}

		private int RelativeDirectionOfPipeAndConn(Pipe pipe, Connector conn)
		{
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			XYZ xyz = new XYZ(hypiping.EndConnector.Origin.X, hypiping.EndConnector.Origin.Y, hypiping.StartConnector.Origin.Z);
			XYZ xyz2 = hypiping.StartConnector.Origin.Subtract(xyz).Normalize();
			XYZ basisZ = conn.CoordinateSystem.BasisZ;
			double num = xyz2.AngleTo(basisZ);
			int result;
			if (num <= 0.0001 || Math.Abs(num - Math.PI) <= 0.0001)
			{
				result = 0;
			}
			else if (Math.Abs(num - Math.PI*.5) <= 0.0001 || Math.Abs(num - 4.71238898038469) <= 0.0001)
			{
				result = 1;
			}
			else
			{
				result = 2;
			}
			return result;
		}

		public XYZ GetProjectOnPipe(Pipe pipe, Element elem, double deviation = 0.0001)
		{
			LocationCurve locationCurve = pipe.Location as LocationCurve;
			LocationPoint locationPoint = this.GetLocationPoint(elem);
			return this.Project(locationPoint.Point, locationCurve.Curve, deviation);
		}

		public XYZ GetProjectOnPipe(Pipe pipe, XYZ connPoint)
		{
			Location location = pipe.Location;
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			YJKLine hyline = new YJKLine(hypiping.StartConnector.Origin, hypiping.EndConnector.Origin);
			hyline.MakeUnBound();
			return hyline.GetProject(connPoint);
		}

		public LocationPoint GetLocationPoint(Element elem)
		{
			return elem.Location as LocationPoint;
		}

		public Connector GetYJKElementConnector(Element elem)
		{
			Connector result = null;
			if (elem is FamilyInstance)
			{
				ConnectorSetIterator connectorSetIterator = (elem as FamilyInstance).MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null)
					{
						result = connector;
						break;
					}
				}
			}
			return result;
		}

		public IList<Connector> GetYJKElementConnector(FamilyInstance elem, Domain domain)
		{
			IList<Connector> list = new List<Connector>();
			if (elem != null)
			{
				ConnectorSetIterator connectorSetIterator = elem.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && connector.Domain == domain)
					{
						list.Add(connector);
					}
				}
			}
			return list;
		}

		public Connector GetTeeMainConnector(FamilyInstance fi)
		{
			Connector result = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(-1.0, 0.0, 0.0)))
					{
						result = connector;
					}
				}
			}
			return result;
		}

		public Connector GetTeeOutConnector(FamilyInstance fi, XYZ vector)
		{
			Connector result = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-vector))
					{
						result = connector;
					}
				}
			}
			return result;
		}

		public Connector GetTeeSideConnector(FamilyInstance fi, XYZ vector)
		{
			Connector result = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-vector) && !connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(vector))
					{
						result = connector;
					}
				}
			}
			return result;
		}

		public Connector GetNearConnector(FamilyInstance fi, Connector connector)
		{
			Connector connector2 = null;
			if (fi != null)
			{
				ConnectorSetIterator connectorSetIterator = fi.MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector3 = obj as Connector;
					if (connector3 != null && connector != null)
					{
						if (connector2 != null)
						{
							if (connector3.Origin.DistanceTo(connector.Origin) < connector2.Origin.DistanceTo(connector.Origin))
							{
								connector2 = connector3;
							}
						}
						else
						{
							connector2 = connector3;
						}
					}
				}
			}
			return connector2;
		}

		public IList<AutoConnectParameter> OrderByDistanceOfEnd(XYZ point, XYZ endPoint, IList<AutoConnectParameter> listParam)
		{
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				if (autoConnectParameter.PointOnDuct != null)
				{
					autoConnectParameter.DistanceOfEnd = autoConnectParameter.PointOnDuct.DistanceTo(point);
				}
				else
				{
					XYZ point2;
					if (autoConnectParameter.LocationPoint != null)
					{
						point2 = autoConnectParameter.LocationPoint.Point;
					}
					else
					{
						point2 = autoConnectParameter.PipePoint;
					}
					YJKLine hyline = new YJKLine(point, endPoint);
					hyline.MakeUnBound();
					autoConnectParameter.PointOnDuct = hyline.GetProject(point2);
					autoConnectParameter.DistanceOfEnd = autoConnectParameter.PointOnDuct.DistanceTo(point);
					if (autoConnectParameter.PointOnFace != null && autoConnectParameter.PointOnDuct != null)
					{
						autoConnectParameter.VectorToDuct = autoConnectParameter.PointOnDuct - autoConnectParameter.PointOnFace;
					}
				}
			}
			IList<AutoConnectParameter> list = (from c in listParam
			orderby c.DistanceOfEnd
			select c).ToList<AutoConnectParameter>();
			int num = 1;
			for (int i = 0; i < list.Count; i++)
			{
				if (i == 0)
				{
					list[i].IndexOfDuct = num;
				}
				else if (Math.Abs(list[i].DistanceOfEnd - list[i - 1].DistanceOfEnd) < 0.33)
				{
					list[i].IndexOfDuct = num;
				}
				else
				{
					num++;
					list[i].IndexOfDuct = num;
				}
			}
			return list;
		}

		public IList<FanCoilParameter> OrderByDistanceOfEnd(XYZ point, IList<FanCoilParameter> listParam)
		{
			foreach (FanCoilParameter fanCoilParameter in listParam)
			{
				if (fanCoilParameter.PointOnPipe != null)
				{
					fanCoilParameter.DistanceOfEnd = fanCoilParameter.PointOnPipe.DistanceTo(point);
				}
			}
			IList<FanCoilParameter> list = (from c in listParam
			orderby c.DistanceOfEnd
			select c).ToList<FanCoilParameter>();
			int num = 1;
			for (int i = 0; i < list.Count; i++)
			{
				if (i == 0)
				{
					list[i].IndexOfPipe = num;
				}
				else if (Math.Abs(list[i].DistanceOfEnd - list[i - 1].DistanceOfEnd) < 0.032808398950131233)
				{
					list[i].IndexOfPipe = num;
				}
				else
				{
					num++;
					list[i].IndexOfPipe = num;
				}
			}
			return list;
		}

		public bool AlignElement(XYZ point, XYZ endPoint, Element elem)
		{
			bool result = false;
			YJKLine hyline = new YJKLine(point, endPoint);
			Location location = elem.Location;
			XYZ point2 = (location as LocationPoint).Point;
			XYZ xyz = new XYZ(point2.X, point2.Y, point.Z);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(xyz);
			double distance = hyline.GetDistance(xyz);
			if (distance > 0.001)
			{
				XYZ xyz2 = distance * (project - xyz).Normalize();
				location.Move(xyz2);
				result = true;
			}
			return result;
		}

		public bool AlignElement(XYZ point, XYZ endPoint, FanCoilParameter fcParam, Autodesk.Revit.DB.Document doc)
		{
			bool result = false;
			YJKLine hyline = new YJKLine(point, endPoint);
			Location location = doc.GetElementById(fcParam.ElementId).Location;
			XYZ extendPoint = fcParam.ExtendPoint;
			XYZ xyz = new XYZ(extendPoint.X, extendPoint.Y, point.Z);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(xyz);
			double distance = hyline.GetDistance(xyz);
			if (distance > 0.001)
			{
				XYZ xyz2 = distance * (project - xyz).Normalize();
				location.Move(xyz2);
				result = true;
			}
			return result;
		}

		public bool AlignElement(XYZ point, XYZ endPoint, Element elem, Element elem2)
		{
			YJKLine hyline = new YJKLine(point, endPoint);
			Location location = elem.Location;
			XYZ point2 = (location as LocationPoint).Point;
			new XYZ(point2.X, point2.Y, point.Z);
			XYZ point3 = (elem2.Location as LocationPoint).Point;
			new XYZ(point3.X, point3.Y, point.Z);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(point2);
			XYZ project2 = hyline.GetProject(point3);
			XYZ xyz = project.DistanceTo(project2) * (project2 - project).Normalize();
			location.Move(xyz);
			return true;
		}

		public bool AlignElement(XYZ point, XYZ endPoint, List<Element> elems)
		{
			bool result = false;
			if (elems == null || elems.Count < 2)
			{
				return result;
			}
			YJKLine hyline = new YJKLine(point, endPoint);
			hyline.MakeUnBound();
			Location location = elems[0].Location;
			XYZ xyz = new XYZ();
			if (!(location is LocationPoint))
			{
				xyz = ((Line)((LocationCurve)location).Curve).Origin;
			}
			else
			{
				xyz = (location as LocationPoint).Point;
			}
			new XYZ(xyz.X, xyz.Y, point.Z);
			for (int i = 1; i < elems.Count; i++)
			{
				Location location2 = elems[i].Location;
				XYZ xyz2 = new XYZ();
				if (!(location2 is LocationPoint))
				{
					xyz2 = ((Line)((LocationCurve)location2).Curve).Origin;
				}
				else
				{
					xyz2 = (location2 as LocationPoint).Point;
				}
				new XYZ(xyz2.X, xyz2.Y, point.Z);
				XYZ project = hyline.GetProject(xyz);
				XYZ project2 = hyline.GetProject(xyz2);
				XYZ xyz3 = project.DistanceTo(project2) * (project - project2).Normalize();
				location2.Move(xyz3);
			}
			return true;
		}

		public bool AlignElement(XYZ point, XYZ endPoint, List<FanCoilParameter> fcParams, Autodesk.Revit.DB.Document doc)
		{
			bool result = false;
			if (fcParams == null || fcParams.Count < 2)
			{
				return result;
			}
			YJKLine hyline = new YJKLine(point, endPoint);
			hyline.MakeUnBound();
			Location location = doc.GetElementById(fcParams[0].ElementId).Location;
			XYZ extendPoint = fcParams[0].ExtendPoint;
			new XYZ(extendPoint.X, extendPoint.Y, point.Z);
			for (int i = 1; i < fcParams.Count; i++)
			{
				Location location2 = doc.GetElementById(fcParams[i].ElementId).Location;
				XYZ extendPoint2 = fcParams[i].ExtendPoint;
				new XYZ(extendPoint2.X, extendPoint2.Y, point.Z);
				XYZ project = hyline.GetProject(extendPoint);
				XYZ project2 = hyline.GetProject(extendPoint2);
				XYZ xyz = project.DistanceTo(project2) * (project - project2).Normalize();
				location2.Move(xyz);
			}
			return true;
		}

		public IList<Connector> GetDuctConnector(Duct duct)
		{
			IList<Connector> list = new List<Connector>();
			ConnectorSetIterator connectorSetIterator = duct.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (connector != null)
				{
					list.Add(connector);
				}
			}
			return list;
		}

		public IList<Connector> GetDuctConnectorEnd(Duct duct)
		{
			IList<Connector> list = new List<Connector>();
			ConnectorSetIterator connectorSetIterator = duct.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (connector != null && (int)connector.ConnectorType == 1)
				{
					list.Add(connector);
				}
			}
			return list;
		}

		public IList<Connector> GetFlexDuctConnectorEnd(FlexDuct flexDuct)
		{
			IList<Connector> list = new List<Connector>();
			ConnectorSetIterator connectorSetIterator = flexDuct.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (connector != null && (int)connector.ConnectorType == 1)
				{
					list.Add(connector);
				}
			}
			return list;
		}

		public IList<Connector> GetPipeConnectorEnd(Pipe pipe)
		{
			IList<Connector> list = new List<Connector>();
			ConnectorSetIterator connectorSetIterator = pipe.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector connector = obj as Connector;
				if (connector != null && (int)connector.ConnectorType == 1)
				{
					list.Add(connector);
				}
			}
			return list;
		}

		public IList<AutoConnectParameter> OrderByOfVector(IList<AutoConnectParameter> listParam)
		{
			IList<AutoConnectParameter> list = new List<AutoConnectParameter>();
			XYZ xyz = new XYZ();
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				if (autoConnectParameter.VectorToDuct != null)
				{
					bool flag = false;
					xyz = autoConnectParameter.VectorToDuct.Normalize();
					double length = autoConnectParameter.VectorToDuct.GetLength();
					if (list.Count < 1)
					{
						list.Add(autoConnectParameter);
						flag = true;
					}
					else
					{
						AutoConnectParameter autoConnectParameter2 = null;
						foreach (AutoConnectParameter autoConnectParameter3 in list)
						{
							if (autoConnectParameter3.VectorToDuct.Normalize().IsAlmostEqualTo(xyz))
							{
								if (autoConnectParameter3.VectorToDuct.GetLength() < length)
								{
									list.Insert(list.IndexOf(autoConnectParameter3), autoConnectParameter);
									flag = true;
									break;
								}
								autoConnectParameter2 = autoConnectParameter3;
							}
						}
						if (!flag)
						{
							if (autoConnectParameter2 == null)
							{
								list.Add(autoConnectParameter);
							}
							else
							{
								list.Insert(list.IndexOf(autoConnectParameter2) + 1, autoConnectParameter);
							}
						}
					}
				}
			}
			int num = 1;
			for (int i = 0; i < list.Count; i++)
			{
				if (i == 0)
				{
					list[i].IndexOfElement = num;
				}
				else if (i > 0 && list[i].VectorToDuct != null)
				{
					if (list[i].VectorToDuct.Normalize().IsAlmostEqualTo(list[i - 1].VectorToDuct.Normalize()))
					{
						list[i].IndexOfElement = num;
					}
					else
					{
						num = 1;
						list[i].IndexOfElement = num;
					}
				}
				num++;
			}
			return list;
		}

		public IList<FanCoilParameter> OrderByOfVector(IList<FanCoilParameter> listParam)
		{
			IList<FanCoilParameter> list = new List<FanCoilParameter>();
			XYZ xyz = new XYZ();
			foreach (FanCoilParameter fanCoilParameter in listParam)
			{
				if (fanCoilParameter.HorizontalVector != null)
				{
					bool flag = false;
					xyz = fanCoilParameter.HorizontalVector.Normalize();
					double length = fanCoilParameter.HorizontalVector.GetLength();
					if (list.Count < 1)
					{
						list.Add(fanCoilParameter);
						flag = true;
					}
					else
					{
						FanCoilParameter fanCoilParameter2 = null;
						foreach (FanCoilParameter fanCoilParameter3 in list)
						{
							if (fanCoilParameter3.HorizontalVector.Normalize().IsAlmostEqualTo(xyz))
							{
								if (fanCoilParameter3.HorizontalVector.GetLength() < length)
								{
									list.Insert(list.IndexOf(fanCoilParameter3), fanCoilParameter);
									flag = true;
									break;
								}
								fanCoilParameter2 = fanCoilParameter3;
							}
						}
						if (!flag)
						{
							if (fanCoilParameter2 == null)
							{
								list.Add(fanCoilParameter);
							}
							else
							{
								list.Insert(list.IndexOf(fanCoilParameter2) + 1, fanCoilParameter);
							}
						}
					}
				}
			}
			int num = 1;
			for (int i = 0; i < list.Count; i++)
			{
				if (i == 0)
				{
					list[i].IndexOfElement = num;
				}
				else if (i > 0 && list[i].HorizontalVector != null)
				{
					if (list[i].HorizontalVector.Normalize().IsAlmostEqualTo(list[i - 1].HorizontalVector.Normalize()))
					{
						list[i].IndexOfElement = num;
					}
					else
					{
						num = 1;
						list[i].IndexOfElement = num;
					}
				}
				num++;
			}
			return list;
		}

		public IList<List<FanCoilParameter>> OrderByFanCoilOfVector(IList<FanCoilParameter> listParam)
		{
			IList<List<FanCoilParameter>> list = new List<List<FanCoilParameter>>();
			XYZ xyz = null;
			List<FanCoilParameter> list2 = new List<FanCoilParameter>();
			List<FanCoilParameter> list3 = new List<FanCoilParameter>();
			foreach (FanCoilParameter fanCoilParameter in listParam)
			{
				bool flag = false;
				if (fanCoilParameter.HorizontalVector != null)
				{
					if (xyz == null)
					{
						xyz = fanCoilParameter.HorizontalVector.Normalize();
					}
					double length = fanCoilParameter.HorizontalVector.GetLength();
					if (list2.Count < 1)
					{
						list2.Add(fanCoilParameter);
					}
					else if (xyz.IsAlmostEqualTo(fanCoilParameter.HorizontalVector.Normalize(), 0.01))
					{
						foreach (FanCoilParameter fanCoilParameter2 in list2)
						{
							if (fanCoilParameter2.HorizontalVector.GetLength() < length)
							{
								list2.Insert(list2.IndexOf(fanCoilParameter2), fanCoilParameter);
								flag = true;
								break;
							}
						}
						if (!flag)
						{
							list2.Add(fanCoilParameter);
						}
					}
					else if (list3.Count < 1)
					{
						list3.Add(fanCoilParameter);
					}
					else
					{
						foreach (FanCoilParameter fanCoilParameter3 in list3)
						{
							if (fanCoilParameter3.HorizontalVector.GetLength() < length)
							{
								list3.Insert(list3.IndexOf(fanCoilParameter3), fanCoilParameter);
								flag = true;
								break;
							}
						}
						if (!flag)
						{
							list3.Add(fanCoilParameter);
						}
					}
				}
			}
			int num = 1;
			for (int i = 0; i < list2.Count; i++)
			{
				list2[i].IndexOfElement = num;
				num++;
			}
			num = 1;
			for (int j = 0; j < list3.Count; j++)
			{
				list3[j].IndexOfElement = num;
				num++;
			}
			if (list2.Count > 0)
			{
				list.Add(list2);
			}
			if (list3.Count > 0)
			{
				list.Add(list3);
			}
			return list;
		}

		public IList<AutoConnectParameter> PositionAssign(IList<AutoConnectParameter> listParam, XYZ point, XYZ endPoint)
		{
			double num = point.DistanceTo(endPoint);
			foreach (AutoConnectParameter autoConnectParameter in listParam)
			{
				if (autoConnectParameter.VectorToDuct == null)
				{
					autoConnectParameter.PositionFlag = 2;
				}
				else if (autoConnectParameter.VectorToDuct.GetLength() < 0.33)
				{
					autoConnectParameter.PositionFlag = 1;
					if (autoConnectParameter.DistanceOfEnd < 0.33 || Math.Abs(autoConnectParameter.DistanceOfEnd - num) < 0.33)
					{
						autoConnectParameter.PositionFlag = 2;
					}
				}
				else
				{
					autoConnectParameter.PositionFlag = 3;
				}
			}
			return listParam;
		}

		public IList<FanCoilParameter> PositionAssign(IList<FanCoilParameter> listParam, XYZ point, XYZ endPoint)
		{
			double num = point.DistanceTo(endPoint);
			foreach (FanCoilParameter fanCoilParameter in listParam)
			{
				if (fanCoilParameter.HorizontalVector.GetLength() < 0.065616797900262466)
				{
					fanCoilParameter.PositionFlag = 1;
					if (fanCoilParameter.DistanceOfEnd < 0.16404199475065617 || Math.Abs(fanCoilParameter.DistanceOfEnd - num) < 0.16404199475065617)
					{
						fanCoilParameter.PositionFlag = 2;
					}
				}
				else
				{
					if (fanCoilParameter.DistanceOfEnd < 0.16404199475065617 || Math.Abs(fanCoilParameter.DistanceOfEnd - num) < 0.16404199475065617)
					{
						fanCoilParameter.PositionFlag = 2;
					}
					fanCoilParameter.PositionFlag = 3;
				}
			}
			return listParam;
		}

		public bool IsElementAlign(IList<AutoConnectParameter> listParam)
		{
			bool result = true;
			double num = 0.0;
			int num2 = 0;
			while (num2 < listParam.Count && num2 + 1 < listParam.Count)
			{
				for (int i = num2 + 1; i < listParam.Count; i++)
				{
					if (Math.Abs(listParam[num2].DistanceOfEnd - listParam[i].DistanceOfEnd) > num)
					{
						num = Math.Abs(listParam[num2].DistanceOfEnd - listParam[i].DistanceOfEnd);
					}
				}
				num2++;
			}
			if (num > 0.0001)
			{
				result = false;
			}
			return result;
		}

		public bool IsElementAlign(IList<FanCoilParameter> listParam)
		{
			bool result = true;
			double num = 0.0;
			int num2 = 0;
			while (num2 < listParam.Count && num2 + 1 < listParam.Count)
			{
				for (int i = num2 + 1; i < listParam.Count; i++)
				{
					if (Math.Abs(listParam[num2].DistanceOfEnd - listParam[i].DistanceOfEnd) > num)
					{
						num = Math.Abs(listParam[num2].DistanceOfEnd - listParam[i].DistanceOfEnd);
					}
				}
				num2++;
			}
			if (num > 0.0001)
			{
				result = false;
			}
			return result;
		}

		public bool IsSupportConnect(Connector ductConn, Connector terminalConn)
		{
			return ductConn != null && terminalConn != null && ductConn.Shape != ConnectorSharp.Oval && ductConn.Shape == terminalConn.Shape && ductConn.Shape != ConnectorSharp.Invalid && terminalConn.Shape != ConnectorSharp.Invalid;
		}
	}
}
